package net.zdreamer.geracomium.ui;

import java.util.ArrayList;
import java.util.List;

import net.zdreamer.geracomium.adapter.GeracomiumIntroViewPagerAdapter;
import net.zdreamer.geracomium.adapter.GeracomiumIntroViewPagerAdapter.OnItemClickListener;
import net.zdreamer.geracomium.myviews.CustomViewPager;
import net.zdreamer.geracomium.ui.home.AssessActivity;
import net.zdreamer.geracomium.ui.home.CommentActivity;
import net.zdreamer.geracomium.ui.home.GeracomiunInfosActivity;
import net.zdreamer.geracomium.utils.window.ViewControlor;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.geracomium.R;
import com.umeng.socialize.controller.UMSocialService;

public class GeracomiumIntroFragment extends Fragment implements
        View.OnClickListener, OnPageChangeListener {
    private CustomViewPager customViewPager;
    private GeracomiumIntroViewPagerAdapter vp_adapter;
    private LinearLayout deviceInGeracomium, surrounding, serviceGuide, lay;
    private RelativeLayout vp_home;
    private RatingBar ratingBar;
    private TextView intro, phone, address;
    private ImageView comment, share, assess;
    private int count, index = 0;
    private List<String> list;// vp
    private List<ImageView> list_vs;// ��
    private SelectPicPopupWindow pop;
    private UMSocialService mController;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_intro, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initViews();

        ViewControlor.setSize(this.getActivity(), vp_home, 12, 5, true);
    }

    private void initViews() {
        comment = (ImageView) getActivity().findViewById(R.id.bottom_comment);
        comment.setOnClickListener(this);
        share = (ImageView) getActivity().findViewById(R.id.bottom_share);
        share.setOnClickListener(this);
        assess = (ImageView) getActivity().findViewById(R.id.bottom_assess);
        assess.setOnClickListener(this);

        deviceInGeracomium = (LinearLayout) getActivity().findViewById(
                R.id.device_in_geracomium);
        deviceInGeracomium.setOnClickListener(this);
        surrounding = (LinearLayout) getActivity().findViewById(
                R.id.surrounding);
        surrounding.setOnClickListener(this);
        serviceGuide = (LinearLayout) getActivity().findViewById(
                R.id.service_guide);
        serviceGuide.setOnClickListener(this);
        vp_home = (RelativeLayout) getActivity().findViewById(
                R.id.viewpager_home);
        lay = (LinearLayout) getActivity().findViewById(R.id.assss);
        customViewPager = (CustomViewPager) getActivity().findViewById(
                R.id.viewpager_fragment_intro);
        customViewPager.setOnPageChangeListener(this);
        initViewPager(customViewPager);// ���ù��ķ���
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(3000);
                        Message msg = new Message();
                        msg.what = 0;
                        handler.sendMessage(msg);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        ratingBar = (RatingBar) getActivity().findViewById(R.id.ratingBar1);
        intro = (TextView) getActivity().findViewById(
                R.id.textview_frament_intro);
        phone = (TextView) getActivity().findViewById(R.id.phone_geracomium);
        address = (TextView) getActivity()
                .findViewById(R.id.address_geracomium);
        initSize();
    }

    private void initViewPager(final ViewPager vp_home2) {
        count = 5;// jsonArray.length();
        list_vs = new ArrayList<ImageView>();
        drawDot(count, 0, false);// boolean is it the first time to draw dots

        vp_adapter = new GeracomiumIntroViewPagerAdapter(
                GeracomiumIntroFragment.this);

        vp_adapter.setOnItemOnClickListener(new OnItemClickListener() {
            @Override
            public void onItemClickListener(int position) {
            }
        });
        // vp_adapter.bindData(list);
        vp_home2.setAdapter(vp_adapter);
        vp_adapter.notifyDataSetChanged();
    }

    private void drawDot(int cc, int cur, boolean f) {
        for (int j = 0; j < cc; j++) {
            if (f) {
                if (j == cur) {
                    list_vs.get(j).setImageResource(R.drawable.page_now);
                } else {
                    list_vs.get(j).setImageResource(R.drawable.page);
                }
            } else {
                ImageView v = new ImageView(this.getActivity());
                v.setPadding(3, 0, 3, 0);
                if (j == 0) {
                    v.setImageResource(R.drawable.page_now);
                } else {
                    v.setImageResource(R.drawable.page);
                }
                list_vs.add(v);
                lay.addView(v);
            }
        }
    }

    private final Handler handler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 0:
                    index++;
                    customViewPager.setCurrentItem(index % list_vs.size());
                    break;

                default:
                    break;
            }
        }

        ;
    };

    private void showPop() {
        pop = new SelectPicPopupWindow(getActivity());
        pop.setAnimationStyle(R.style.popwin_anim_style);
        // ���봰�������ļ�
        View view = View.inflate(this.getActivity(), R.layout.popwindow_shara,
                null);
        view.findViewById(R.id.share_sina).setOnClickListener(pop_click);
        view.findViewById(R.id.share_tencent).setOnClickListener(pop_click);
        view.findViewById(R.id.share_wechat).setOnClickListener(pop_click);
        view.findViewById(R.id.pop_undo).setOnClickListener(pop_click);
        pop.init(view).showAtLocation(getActivity().findViewById(R.id.pop_bg),
                Gravity.CENTER, 0, 0);
    }

    public class SelectPicPopupWindow extends PopupWindow {

        public SelectPicPopupWindow(Activity context) {
            super(context);
        }

        public SelectPicPopupWindow init(View view) {
            this.setContentView(view);
            this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
            this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
            this.setBackgroundDrawable(new ColorDrawable(0xb0000000));
            setFocusable(true);
            return this;
        }

    }

    OnClickListener pop_click = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.share_sina:

                    break;

                case R.id.share_tencent:

                    break;

                case R.id.share_wechat:

                    break;

                case R.id.pop_undo:
                    pop.dismiss();
                    break;

                default:
                    break;
            }
        }

    };

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getActivity(), GeracomiunInfosActivity.class);
        switch (v.getId()) {
            case R.id.device_in_geracomium:
                intent.putExtra("witch", "1");
                startActivity(intent);
                break;

            case R.id.surrounding:
                intent.putExtra("witch", "2");
                startActivity(intent);
                break;

            case R.id.service_guide:
                intent.putExtra("witch", "3");
                startActivity(intent);
                break;

            case R.id.bottom_comment:
                // ��ת���۽���
                startActivity(new Intent(getActivity(), CommentActivity.class));
                break;
            case R.id.bottom_share:
                // ����������
                showPop();
                // mController.openShare(getActivity(), false);
                break;
            case R.id.bottom_assess:
                // ��ת��������
                startActivity(new Intent(getActivity(), AssessActivity.class));
                break;

            default:
                break;
        }
    }

    private void initSize() {
        ImageView imageView = (ImageView) getActivity().findViewById(
                R.id.imageview_toolbar);
        LinearLayout.LayoutParams iv_params = (LayoutParams) imageView
                .getLayoutParams();
        LinearLayout layout = (LinearLayout) getActivity().findViewById(
                R.id.detail_bottom);
        LayoutParams aa = (LayoutParams) layout.getLayoutParams();
        LinearLayout.LayoutParams layoutParams = (LayoutParams) getActivity()
                .findViewById(R.id.bottom_share).getLayoutParams();
        LinearLayout.LayoutParams bb = (LayoutParams) getActivity()
                .findViewById(R.id.ssssssline).getLayoutParams();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenWidth = dm.widthPixels;
        layoutParams.height = screenWidth * 100 / 260 / 3;
        iv_params.width = screenWidth / 2;
        aa.height = layoutParams.height;
        bb.height = screenWidth * 60 / 260 / 3;
        layout.setLayoutParams(aa);
        imageView.setLayoutParams(iv_params);
        getActivity().findViewById(R.id.ssssssline).setLayoutParams(bb);
        getActivity().findViewById(R.id.ssssssline1).setLayoutParams(bb);
        getActivity().findViewById(R.id.bottom_share).setLayoutParams(
                layoutParams);
        getActivity().findViewById(R.id.bottom_assess).setLayoutParams(
                layoutParams);
        getActivity().findViewById(R.id.bottom_comment).setLayoutParams(
                layoutParams);
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    @Override
    public void onPageSelected(int arg0) {
        customViewPager.setCurrentItem(arg0);
        drawDot(list_vs.size(), arg0, true);
    }
}
